package com.UdpRecieve.controller;

import com.UdpRecieve.domain.SysFileType;
import com.UdpRecieve.domain.SysKeyWord;
import com.UdpRecieve.framework.annotation.LogAnnotion;
import com.UdpRecieve.framework.enums.BusinessType;
import com.UdpRecieve.service.SysFileTypeService;
import com.UdpRecieve.service.SysKeyWordService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/ConfigPage")
public class ConfigController {

    @Autowired
    private SysKeyWordService sysKeyWordService;

    @Autowired
    private SysFileTypeService sysFileTypeService;

    @GetMapping()
    public ModelAndView toConfigPage(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("system/option_approve");
        return mv;
    }

    @PostMapping("/list")
    @ResponseBody
    public String  getConfigKeyWord(){
        ModelMap mv = new ModelMap();
        List<SysKeyWord> sysKeyWordList = sysKeyWordService.selectSysKeyWordList();
        mv.addAttribute("rows",sysKeyWordList);
        return JSON.toJSONString(mv);
    }


    @PostMapping("/list02")
    @ResponseBody
    public String  getConfigFileType(){
        ModelMap mv = new ModelMap();
        List<SysFileType> sysFileTypeList = sysFileTypeService.selectSysFileTypeList();
        mv.addAttribute("rows",sysFileTypeList);
        return JSON.toJSONString(mv);
    }


    @PostMapping("/delete")
    @LogAnnotion(title = "审批配置_关键字",businessType = BusinessType.DELETE)
    @ResponseBody
    public ModelMap deleteKeyWords(String ids){
        ModelMap m = new ModelMap();
        int i = -1;
        try {
            i = sysKeyWordService.deleteSysKeyWordListByIds(ids);
        }catch (Exception e){
            i = -1;
            e.printStackTrace();
        }
        m.addAttribute("result",i);
        return m;
    }


    @PostMapping("/delete02")
    @LogAnnotion(title = "审批配置_文件类型",businessType = BusinessType.DELETE)
    @ResponseBody
    public ModelMap deleteFileTypes(String ids){
        ModelMap m = new ModelMap();
        int i = -1;
        try {
            i = sysFileTypeService.deleteSysFileTypeListByIds(ids);
        }catch (Exception e){
            i = -1;
            e.printStackTrace();
        }
        m.addAttribute("result",i);
        return m;
    }

    /**
     * 关键字添加
     * @param sysKeyWord
     * @return
     */
    @PostMapping("/add")
    @LogAnnotion(title = "审批配置_关键字",businessType = BusinessType.INSERT)
    @ResponseBody
    public ModelMap addKeyWord(SysKeyWord sysKeyWord){
        ModelMap m = new ModelMap();
        int i = -1;//-1为插入失败
        try {
            i = sysKeyWordService.initKeyWord(sysKeyWord);
        }catch (Exception e){
            e.printStackTrace();
        }

        m.addAttribute("result",i);
        return m;
    }

    /**
     * 文件类型添加
     * @param sysFileType
     * @return
     */
    @PostMapping("/add02")
    @LogAnnotion(title = "审批配置_文件类型",businessType = BusinessType.INSERT)
    @ResponseBody
    public ModelMap addFileType(SysFileType sysFileType){
        ModelMap m = new ModelMap();
        int i = -1;
        try {
            i = sysFileTypeService.initFileType(sysFileType);
        }catch (Exception e){
            e.printStackTrace();
        }

        m.addAttribute("result",i);
        return m;
    }
}
